
cc.Class({
    extends: cc.Component,

    properties: {
        petProgressBar: cc.ProgressBar,
    },



    onLoad() {
        this.schedule(this.petFire, 0.5);
        this.hp = this.maxHp = 500;
        this.petMove();
        this.schedule(this.petMove, 8);
        //获取子弹管理者的节点
        this.bulletMgrNode = cc.find("Canvas/bulletMgr");
        //宠物扣血的速度
        this.speed = 50;
    },

    start() {

    },

    update(dt) {
        this.hp -= dt * this.speed;
        this.petProgressBar.progress = this.hp / this.maxHp;
        if (this.hp <= 0) {
            this.node.removeFromParent();
        }
    },
    //宠物发射子弹
    petFire() {
        let bulletMgrNodeJs = this.bulletMgrNode.getComponent("BulletMgr");
        let petPosition = cc.v2(this.node.parent.parent.x + this.node.x, this.node.parent.parent.y + this.node.y);
        bulletMgrNodeJs.createBullet(petPosition, "pet", "pet");
    },

    //宠物围着玩家角色移动
    petMove() {
        let x = this.node.parent.width * 2;
        let y = this.node.parent.height * 3;
        cc.tween(this.node)
            .by(2, { position: cc.v2(-x, 0) })
            .by(2, { position: cc.v2(0, -y) })
            .by(2, { position: cc.v2(x, 0) })
            .by(2, { position: cc.v2(0, y) })
            .start()
    }
});
